<?php

namespace app\banner\controller;

use app\common\controller\Adminbase;
use app\banner\model\Banner  as BannerModel;
use think\Db;


class Banner extends Adminbase
{
    protected $modelClass = null;
    protected function initialize()
    {
        parent::initialize();
        $this->modelClass = new BannerModel;
    }
 
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames(); 
            $list = $this->modelClass::where($where)->page($page, $limit)->order('id', 'desc')->select();
			$_list = [];
            foreach ($list as $k => $v) { 
                if (isset($v['image'])) {
                    $v['imgurl'] = get_file_path($v['image']);
                } 
				$v['title'] = ($v['title']=='')?'无标题':$v['title'];
				$v['groupid'] = ($v['groupid'] == 0)?'无分组':(Db::name('Terms')->where(["module" => "banner"])->where('id',$v['groupid'])->value('name'));
                $_list[] = $v;
            }
			$list = $_list; 
			
            $total = $this->modelClass::where($where)->order('id', 'desc')->count();
            $result = array("code" => 0, "count" => $total, "data" => $_list);
            return json($result);
        }
        return $this->fetch();
    }
	
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //验证器
            $rule = [
                'image|图片' => 'require',
            ];
            $validate = new \think\Validate($rule);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            if (!empty($data['group']['name'])) {
                $data['groupid'] = $this->addGroup($data['group']['name']);
            }
            $status = $this->modelClass->allowField(true)->save($data);
            if ($status) {
                $this->success("添加成功！", url("banner/index"));
            } else {
                $this->error("添加失败！");
            }
        } else {
            $Group = Db::name('Terms')->where(["module" => "banner"])->select();
            $this->assign("Group", $Group);
            return $this->fetch();
        }
    }

    /**
     * 编辑
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //验证器
            $rule = [
                'title' => 'require', 
            ];
            $msg = [
                'title.require' => '图片名称不得为空',
            ];
            $validate = new \think\Validate($rule, $msg);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            if (!empty($data['group']['name'])) {
                $data['groupid'] = $this->addGroup($data['group']['name']);
            }
            $status = $this->modelClass->allowField(true)->save($data, ['id' => $data['id']]);
            if ($status) {
                $this->success("编辑成功！", url("banner/index"));
            } else {
                $this->error("编辑失败！");
            }

        } else {
            $id = $this->request->param('id', 0);
            $data = $this->modelClass->where(array("id" => $id))->find();
            if (!$data) {
                $this->error("该信息不存在！");
            }
            $group = Db::name('Terms')->where(["module" => "banner"])->select();
            $this->assign("group", $group);
            $this->assign("data", $data);
            return $this->fetch();
        }

    }

    //分组管理
    public function group()
    {
        if ($this->request->isAjax()) {
            $_list = Db::name('Terms')->where(["module" => "banner"])->select();
            $total = count($_list);
            $result = array("code" => 0, "count" => $total, "data" => $_list);
            return json($result);
        }
        return $this->fetch("group");
    }

    //分组编辑
    public function editGroup()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post('');
            if (Db::name('Terms')->update($data) !== false) {
                $this->success("更新成功！", url("banner/banner/group"));
            } else {
                $this->error("更新失败！");
            }
        } else {
            $id = $this->request->param('id/d', 0);
            $info = Db::name('Terms')->where(["id" => $id, "module" => "banner"])->find();
            if (!$info) {
                $this->error("该分组不存在！");
            }
            $this->assign('data', $info);
            return $this->fetch("editgroup");
        }
    }

    /**
     * 添加分组
     * @param type $name
     */
    protected function addGroup($name)
    {
        $name = trim($name);
        if (empty($name)) {
            $this->error("分组名称不能为空！");
        }
        $count = Db::name('Terms')->where(["name" => $name, "module" => "banner"])->count();
        if ($count > 0) {
            $this->error("该分组已经存在！");
        }
        $TermsId = Db::name('Terms')->insertGetId(["name" => $name, "module" => "banner"]);
        if ($TermsId) {
            return $TermsId;
        } else {
            $this->error("分组添加失败！");
        }
    }

    //分类删除
    public function deleteGroup()
    {
        $id = $this->request->param('id/d', 0);
        if (Db::name('Terms')->where(["id" => $id, "module" => "banner"])->delete()) {
            Db::name('Banner')->where(["groupid" => $id])->delete();
            $this->success("分组删除成功！");
        } else {
            $this->error("分组删除失败！");
        }
    }


}
